package com.codedancer.campus.manager.api.updateLog;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.codedancer.campus.common.anno.InterfacePermission;
import com.codedancer.campus.common.core.BaseApi;
import com.codedancer.campus.common.response.ApiResponse;
import com.codedancer.campus.manager.api.updateLog.ro.UpdateLogAddOrUpdateRo;
import com.codedancer.campus.manager.api.updateLog.ro.UpdateLogPageSearchRo;
import com.codedancer.campus.manager.service.IUpdateLogService;
import com.codedancer.campus.manager.service.bo.updateLog.UpdateLogAddOrUpdateBo;
import com.codedancer.campus.manager.service.bo.updateLog.UpdateLogPageSearchBo;
import com.codedancer.campus.manager.service.vo.updateLog.UpdateLogPageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/update-log")
@CrossOrigin(methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.DELETE,
    RequestMethod.PUT}, maxAge = 3600)
@Api(value = "更新日志Api", description = "更新日志Api", tags = "更新日志Api")
public class UpdateLogApi extends BaseApi {

    @Autowired
    private IUpdateLogService updateLogService;

    @PostMapping("/add")
    @ApiOperation("添加更新日志")
    @InterfacePermission(encodes = {"superAdmin"})
    public ApiResponse<Void> add(
        @RequestBody @Valid @ApiParam(value = "更新日志添加Ro", required = true) UpdateLogAddOrUpdateRo updateLogAddOrUpdateRo) {
        updateLogService.add(
            BeanUtil.copyProperties(updateLogAddOrUpdateRo, UpdateLogAddOrUpdateBo.class));
        return success();
    }

    @PutMapping("/update/{uuid}")
    @ApiOperation("修改更新日志")
    @InterfacePermission(encodes = {"superAdmin"})
    public ApiResponse<Void> update(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "更新日志uuid", required = true) String uuid,
        @RequestBody @Valid @ApiParam(value = "更新日志修改Ro", required = true) UpdateLogAddOrUpdateRo updateLogAddOrUpdateRo) {
        UpdateLogAddOrUpdateBo updateLogAddOrUpdateBo = BeanUtil.copyProperties(
            updateLogAddOrUpdateRo, UpdateLogAddOrUpdateBo.class);
        updateLogAddOrUpdateBo.setUuid(uuid);
        updateLogService.update(updateLogAddOrUpdateBo);
        return success();
    }

    @GetMapping("/page")
    @ApiOperation("分页查询更新日志")
    @InterfacePermission(encodes = {"superAdmin"})
    public ApiResponse<IPage<UpdateLogPageVo>> page(
        @ApiParam(value = "更新日志分页查询Ro", required = false)
        UpdateLogPageSearchRo updateLogPageSearchRo) {
        UpdateLogPageSearchBo updateLogPageSearchBo = BeanUtil.copyProperties(updateLogPageSearchRo,
            UpdateLogPageSearchBo.class);
        return success(updateLogService.page(updateLogPageSearchBo));
    }

    @DeleteMapping("/delete/{uuid}")
    @ApiOperation("删除更新日志")
    @InterfacePermission(encodes = {"superAdmin"})
    public ApiResponse<Void> delete(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "更新日志uuid", required = true) String uuid) {
        updateLogService.delete(uuid);
        return success();
    }

    @PutMapping("/push/{uuid}")
    @ApiOperation("推送更新日志")
    @InterfacePermission(encodes = {"superAdmin"})
    public ApiResponse<Void> push(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "更新日志uuid", required = true) String uuid) {
        updateLogService.push(uuid);
        return success();
    }
}
